package com.nova.scenic.projectlibs.util.graphics;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Typeface;
import android.widget.TextView;
import com.nova.scenic.projectlibs.util.StringUtil;

public class FontUtil {
	

	public static int getTextHeight(Paint mTextPaint) {
		FontMetrics fm = mTextPaint.getFontMetrics();
		return (int) Math.ceil(fm.descent - fm.ascent) ;
	}
	
	
	public static int getTextWidth(Paint mTextPaint,CharSequence str) {
        if(StringUtil.isStringEmpty(str)){
            return 0;
        }
        int iRet = 0;  
        if (str != null && str.length() > 0) {  
            int len = str.length();  
            float[] widths = new float[len];  
            mTextPaint.getTextWidths(str.toString(), widths);
            for (int j = 0; j < len; j++) {  
                iRet += (int) Math.ceil(widths[j]);  
            }  
        }  
        return iRet;  
    }  
	
	public static void debugFontMetrics(Canvas canvas, int width) {
		Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setTextSize(60);
		textPaint.setColor(Color.WHITE);

		// FontMetrics对象
		FontMetrics fontMetrics = textPaint.getFontMetrics();
		String text = "abcdefghijklmnopqrstu";

		// 计算每一个坐标
		float baseX = 200;
		float baseY = 800;
		float topY = baseY + fontMetrics.top;
		float ascentY = baseY + fontMetrics.ascent;
		float descentY = baseY + fontMetrics.descent;
		float bottomY = baseY + fontMetrics.bottom;

		// 绘制文本
		canvas.drawText(text, baseX, baseY, textPaint);

		// BaseLine描画
		Paint baseLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		baseLinePaint.setColor(Color.RED);
		canvas.drawLine(0, baseY, width, baseY, baseLinePaint);

		// Base描画
		canvas.drawCircle(baseX, baseY, 5, baseLinePaint);

		// TopLine描画
		Paint topLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		topLinePaint.setColor(Color.BLACK);
		canvas.drawLine(0, topY, width, topY, topLinePaint);

		// AscentLine描画
		Paint ascentLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		ascentLinePaint.setColor(Color.BLUE);
		canvas.drawLine(0, ascentY, width, ascentY, ascentLinePaint);

		// DescentLine描画
		Paint descentLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		descentLinePaint.setColor(Color.YELLOW);
		canvas.drawLine(0, descentY, width, descentY, descentLinePaint);

		// ButtomLine描画
		Paint bottomLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		bottomLinePaint.setColor(Color.WHITE);
		canvas.drawLine(0, bottomY, width, bottomY, bottomLinePaint);
	}


	public static Typeface createTypeface(TextView beSetView,String assetFontPath){
		try {
			Typeface typeFace =Typeface.createFromAsset(beSetView.getContext().getAssets(),
					assetFontPath);
			beSetView.setTypeface(typeFace);
            return typeFace;
		} catch (Exception e) {
		}
        return null;
	}
    public static Typeface createTypeface(Context context,String assetFontPath){
        try {
            Typeface typeFace =Typeface.createFromAsset(context.getAssets(),
                    assetFontPath);
            return typeFace;
        } catch (Exception e) {
        }

        return null;
    }
}
